import { useState, useEffect } from 'react';
import useThrottleFn from '../use-throttle-fn';
import { ThrottleOptions } from './throttleOptions';

export default <T>(value: T, options?: ThrottleOptions) => {
    const [throttled, setThrottled] = useState(value)
    
    const { run } = useThrottleFn(() => {
        setThrottled(value)
    }, options)

    useEffect(() => {
        run()
    }, [value])

    return throttled
}